package com.hui.web.servlet;

import com.hui.utils.UUIDUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/07/12/11:56
 * @Description: 文件上传测试
 */
@WebServlet("/upload")
public class UploadServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            // 创建磁盘文件项工厂
            DiskFileItemFactory factory = new DiskFileItemFactory();
            // 创建文件上传的核心类
            ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setHeaderEncoding("UTF-8");  // 设置编码
            boolean flag = upload.isMultipartContent(req);     // 是否是一个文件上传的表单
            if (flag) {
                //解析request-- - 获得文件项集合
                List<FileItem> itemList = upload.parseRequest(req);
                // 遍历文件项集合
                if (itemList != null) {
                    for (FileItem item : itemList) {
                        boolean b = item.isFormField();
                        if (b) {
                            //  普通表单项
                            String fieldName = item.getFieldName();   // 获取表单的键
                            String value = item.getString("UTF-8");// 换取表单的值
                            System.out.println(fieldName + "=" + value);
                        } else {
                            // 文件上传项
                            String fileName = item.getName(); // 换取文件的名
                            // 通过UUID 换取一个新的文件名
                            String newFileName = UUIDUtils.getUUID() + fileName;
                            // 获取文件的输入流
                            InputStream in = item.getInputStream();

                            // 将文件保存到tomcat中webapp下的upload文件夹中
                            // 获取项目的实际路径  C:\apache-tomcat-8.5.55\webapps\lagou_edu_home
                            String realPath = this.getServletContext().getRealPath("/");
                            // 将 lagou_edu_home截取掉，获取 C:\apache-tomcat-8.5.55\webapps\
                            String path = realPath.substring(0, realPath.indexOf("lagou_edu_home"));

                            // 创建一个输出流
                            FileOutputStream out = new FileOutputStream(path + "/upload/" + newFileName);
                            // 文件传输
                            in.transferTo(out);
                            // 关闭流
                            out.close();
                            in.close();
                        }
                    }
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
